<%--
  Created by IntelliJ IDEA.
  User: Mac
  Date: 2020/12/24
  Time: 10:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%String path = request.getContextPath()+"/";%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=path%>">
    <meta charset="utf-8">
    <title>用户总数--layui后台管理模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
    <link rel="stylesheet" href="layui/css/user.css" media="all" />
</head>
<body class="childrenBody">
<form action="/user/data" type="post" id="form1">
<blockquote class="layui-elem-quote news_search">

        <input type="hidden" name="pageNumber" id="pageNumber"/>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <input name="realName" type="text" value="${user.realName}" placeholder="请输入关键字" class="layui-input search_input">
        </div>
        <shiro:hasPermission name="user:select">
            <input type="submit" class="layui-btn search_btn" value="查询"/>
        </shiro:hasPermission>

    </div>
    <div class="layui-inline">
<shiro:hasPermission name="user:add">
        <a class="layui-btn layui-btn-normal usersAdd_btn">添加用户</a>
</shiro:hasPermission>
    </div>
    <div class="layui-inline">
<shiro:hasPermission name="user:deletes">
        <a class="layui-btn layui-btn-danger batchDel">批量删除</a>
</shiro:hasPermission>
    </div>

</blockquote>
<div class="layui-form users_list">
    <table class="layui-table">
        <colgroup>
            <col width="50">
            <col>
            <col width="8%">
            <col width="8%">
            <col width="8%">
            <col width="8%">
            <col width="8%">
            <col width="8%">
            <col width="12%">
            <col width="18%">
        </colgroup>
        <thead>
        <tr>
            <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose" id="allChoose"></th>
            <th>登录名</th>
            <th>姓名</th>
            <th>性别</th>
            <th>联系电话</th>
            <th>状态</th>
            <th>所属部门</th>
            <th>所属角色</th>
            <th>最后登录时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody class="users_content">
        <c:forEach items="${page.list}" var="user" varStatus="t">
            <tr>
                <td><input type="checkbox" value="${user.id}" name="ids" lay-skin="primary" lay-filter="choose" /></td>
                <td>${user.loginAccount}</td>
                <td>${user.realName}</td>
                <td>
               <c:choose>
                       <c:when test="${user.sex==0}">男</c:when>
                        <c:otherwise>女</c:otherwise>
               </c:choose>
                </td>

                <td>${user.phone}</td>
                <td>
                    <input type="checkbox" lay-skin="switch" lay-text="启用|停用"  <c:if test="${user.status==1}">checked</c:if>/>
                </td>
                <td>${user.org.orgName}</td>
                <td>${user.role.roleName}</td>
                <td>${user.lastlogin}</td>
                <td>
                    <shiro:hasPermission name="user:edit">
                  <a class="layui-btn layui-btn-mini users_edit" data-id="${user.id}"><i class="iconfont icon-edit"></i> 编辑</a>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="user:delete">
                    <a class="layui-btn layui-btn-danger layui-btn-mini users_del" data-id="${user.id}"><i class="layui-icon">&#xe640;</i> 删除</a>
                    </shiro:hasPermission>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

</div>
<div id="page" ></div>
</form>
<script type="text/javascript" src="layui/layui/layui.js"></script>
<script type="text/javascript" src="js/user/data.js"></script>
<script type="text/javascript">
    var index;
    layui.config({
        base : "js/"
    }).use(['form','layer','laypage','jquery'],function() {
        $=layui.jquery;
        form = layui.form();
        layui.laypage({
            cont:'page',
            pages:parseInt('${page.pages}'),
            curr:parseInt('${page.pageNum}'),
            groups:6,
            jump:function (obj,first) {
                // first  true代表首次加载数据，当点击按钮时first为false
                if(!first){
                    // 跳转页面
                    var pageNumber = obj.curr;
                    $("#pageNumber").val(pageNumber);
                    // $("#form1").submit();
                }
            }
        });
        // 单个删除
        $(".users_del").click(function(){
            var id = $(this).attr("data-id");
            layer.confirm('确定删除吗？', {icon: 3, title:'提示'}, function(index){
                //do something
                // 发请求删除
                $.getJSON('user/delete?id='+id,function (ret) {
                    if(ret.status){
                        layer.msg('删除成功!',function(){
                            $("#form1").submit();
                        });

                    }else{
                        layer.msg('删除失败！');
                    }
                });
                layer.close(index);
            });
        });
        //全选
        form.on('checkbox(allChoose)', function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
            child.each(function(index, item){
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });
        form.on("checkbox(choose)",function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
            var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
            if(childChecked.length == child.length){
                $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
            }else{
                $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
            }
            form.render('checkbox');
        })
        // 批量删除
        $(".batchDel").click(function(){
          var checkedList =   $("input[type='checkbox'][name='ids']:checked");

          if(checkedList.length==0){
              layer.msg("请选中需要删除的数据?",function () {
                  return;
              })
          }else{
          layer.confirm("确定删除选中数据吗？",function () {

          var param =  $("#form1").serialize();
           $.ajax({
               url:'user/deletes',
               data:param,
               type:'post',
               dataType:'json',
               success:function(ret){
                   if(ret.status){
                       layer.msg('批量删除成功！',function(){
                           $("#form1").submit();
                       })
                   }else{
                       layer.msg("删除失败!");
                   }
               }
           });

          })
          }
        });
        // 用户新增

        $(".usersAdd_btn").click(function(){
            // 同步请求
            // form表单提交到指定的请求
            // document.location.href
            //
            index = layui.layer.open({
                type:2,
                content:'user/add',
                title:'新增用户',
                area:["583px","430px"],
                maxmin:true,
                resize:true
            });
        })
        // 编辑
        $(".users_edit").click(function(){
            var id = $(this).attr("data-id");
            index = layui.layer.open({
                type:2,
                content:'user/add?id='+id,
                title:'编辑用户',
                area:["583px","430px"],
                maxmin:true,
                resize:true
            });
        });
    });

</script>
</body>
</html>
